package com.ssau.expert.gui.master;

import com.ssau.expert.app.DatabaseAPI;
import com.ssau.expert.common.CallableTask;
import com.ssau.expert.common.InputParameters;
import com.ssau.expert.entity.Ship;
import com.ssau.expert.exception.ShipMergeException;
import com.ssau.expert.gui.app.Locator;
import com.ssau.expert.gui.common.MyControls;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Author: Sergey42
 * Date: 12.11.13 12:09
 */
public class ShipEditor extends JFrame {

    private Ship ship;
    private CallableTask task;

    public ShipEditor(Ship ship, CallableTask task) {
        this.ship = ship;
        this.task = task;
        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    private void initComponents() {
        Dimension size = new Dimension(320, 316);
        Dimension screenSize = getToolkit().getScreenSize();
        setSize(size);
        setLocation((screenSize.getSize().width - size.width) / 2, (screenSize.getSize().height - size.height) / 2);
        setTitle("Редактирование корабля");

        int margin = 5;
        int lWidth = 130;
        int tWidth = 170;
        int hHeight = 25;
        int hWidth = lWidth + margin + tWidth;

        JPanel mainLayout = MyControls.createJPanel(this, 0, 0, 600, 800, "");

        JPanel h1 = MyControls.createJPanel(this, 0, 0, hWidth, hHeight, "");
        JLabel nameLabel = MyControls.createLabel("Название", 0, 0);
        JTextField nameTF = MyControls.createJTextField(lWidth + margin, 0, tWidth, hHeight, false);
        nameTF.setText(ship.getShipName().getLocalName());
        h1.add(nameLabel);
        h1.add(nameTF);
        mainLayout.add(h1);

        JPanel h2 = MyControls.createJPanel(this, 0, hHeight, hWidth, hHeight, "");
        JLabel profileLabel = MyControls.createLabel("Профиль", 0, 0);
        JTextField profileTF = MyControls.createJTextField(lWidth + margin, 0, tWidth, hHeight, false);
        profileTF.setText(ship.getShipProfile().getName());
        h2.add(profileLabel);
        h2.add(profileTF);
        mainLayout.add(h2);

        JPanel h3 = MyControls.createJPanel(this, 0, hHeight * 2, hWidth, hHeight * 2, "");
        JLabel authonomy1Label = MyControls.createLabel("Автономность1", 0, 0);
        final JTextField authonomy1TF = MyControls.createJTextField(lWidth + margin, 0, tWidth, hHeight, true);
        JLabel authonomyCF1Label = MyControls.createLabel("АвтономностьCF1", 0, hHeight);
        final JTextField authonomyCF1TF = MyControls.createJTextField(lWidth + margin, hHeight, tWidth, hHeight, true);
        authonomy1TF.setText(String.valueOf(ship.getMaxDistance1()));
        authonomyCF1TF.setText(String.valueOf(ship.getCfMaxDistance1()));
        h3.add(authonomy1Label);
        h3.add(authonomy1TF);
        h3.add(authonomyCF1Label);
        h3.add(authonomyCF1TF);
        mainLayout.add(h3);

        JPanel h4 = MyControls.createJPanel(this, 0, hHeight * 4, hWidth, hHeight * 2, "");
        JLabel authonomy2Label = MyControls.createLabel("Автономность2", 0, 0);
        final JTextField authonomy2TF = MyControls.createJTextField(lWidth + margin, 0, tWidth, hHeight, true);
        JLabel authonomyCF2Label = MyControls.createLabel("АвтономностьCF2", 0, hHeight);
        final JTextField authonomyCF2TF = MyControls.createJTextField(lWidth + margin, hHeight, tWidth, hHeight, true);
        authonomy2TF.setText(ship.getMaxDistance2() != 0 ? String.valueOf(ship.getMaxDistance2()) : null);
        authonomyCF2TF.setText(ship.getCfMaxDistance2() != 0 ? String.valueOf(ship.getCfMaxDistance2()) : null);
        h4.add(authonomy2Label);
        h4.add(authonomy2TF);
        h4.add(authonomyCF2Label);
        h4.add(authonomyCF2TF);
        mainLayout.add(h4);


        JPanel h5 = MyControls.createJPanel(this, 0, hHeight * 6, hWidth, hHeight * 2, "");
        JLabel personal1Label = MyControls.createLabel("Персонал1", 0, 0);
        final JTextField personal1TF = MyControls.createJTextField(lWidth + margin, 0, tWidth, hHeight, true);
        JLabel personalCF1Label = MyControls.createLabel("ПерсоналCF1", 0, hHeight);
        final JTextField personalCF1TF = MyControls.createJTextField(lWidth + margin, hHeight, tWidth, hHeight, true);
        personal1TF.setText(String.valueOf(ship.getMaxStaff1()));
        personalCF1TF.setText(String.valueOf(ship.getCfMaxStaff1()));
        h5.add(personal1Label);
        h5.add(personal1TF);
        h5.add(personalCF1Label);
        h5.add(personalCF1TF);
        mainLayout.add(h5);

        JPanel h6 = MyControls.createJPanel(this, 0, hHeight * 8, hWidth, hHeight * 2, "");
        JLabel personal2Label = MyControls.createLabel("Персонал2", 0, 0);
        final JTextField personal2TF = MyControls.createJTextField(lWidth + margin, 0, tWidth, hHeight, true);
        JLabel personalCF2Label = MyControls.createLabel("ПерсоналCF2", 0, hHeight);
        final JTextField personalCF2TF = MyControls.createJTextField(lWidth + margin, hHeight, tWidth, hHeight, true);
        personal2TF.setText(ship.getMaxStaff2() != 0 ? String.valueOf(ship.getMaxStaff2()) : null);
        personalCF2TF.setText(ship.getCfMaxStaff2() != 0 ? String.valueOf(ship.getCfMaxStaff2()) : null);
        h6.add(personal2Label);
        h6.add(personal2TF);
        h6.add(personalCF2Label);
        h6.add(personalCF2TF);
        mainLayout.add(h6);

        JButton saveButton = MyControls.createButton("Сохранить", 0, hHeight * 10);
        saveButton.setSize(304, 25);
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                DatabaseAPI databaseAPI = Locator.getDatabase();
                InputParameters params = new InputParameters();

                params.maxDistance1 = isNotBlank(authonomy1TF.getText()) ? Integer.valueOf(authonomy1TF.getText()) : null;
                params.cfMaxDistance1 = isNotBlank(authonomyCF1TF.getText()) ? Integer.valueOf(authonomyCF1TF.getText()) : null;

                params.maxDistance2 = isNotBlank(authonomy2TF.getText()) ? Integer.valueOf(authonomy2TF.getText()) : null;
                params.cfMaxDistance2 = isNotBlank(authonomyCF2TF.getText()) ? Integer.valueOf(authonomyCF2TF.getText()) : null;

                params.staff1 = isNotBlank(personal1TF.getText()) ? Integer.valueOf(personal1TF.getText()) : null;
                params.cfStaff1 = isNotBlank(personalCF1TF.getText()) ? Integer.valueOf(personalCF1TF.getText()) : null;

                params.staff2 = isNotBlank(personal2TF.getText()) ? Integer.valueOf(personal2TF.getText()) : null;
                params.cfStaff2 = isNotBlank(personalCF2TF.getText()) ? Integer.valueOf(personalCF2TF.getText()) : null;

                try {
                    databaseAPI.mergeShip(ship, params);
                    dispose();
                    task.execute();
                } catch (ShipMergeException ex) {
                    JOptionPane.showMessageDialog(ShipEditor.this, ex.getMessage());
                }
            }
        });
        add(saveButton);

        add(mainLayout);

    }

    protected boolean isNotBlank(String s) {
        return s != null && s.length() > 0;
    }


}
